package com.xmmj.common.converter

import com.alibaba.fastjson.serializer.SerializeConfig
import retrofit2.Converter
import okhttp3.RequestBody
import com.alibaba.fastjson.JSON
import okhttp3.MediaType

/**
 * 请求体转换器
 */
class FastJsonRequestBodyConverter<T>(private val serializeConfig: SerializeConfig) :
    Converter<T, RequestBody> {

    override fun convert(value: T): RequestBody {
        return RequestBody.create(MEDIA_TYPE, JSON.toJSONBytes(value, serializeConfig))
    }

    companion object {
        private val MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8")
    }
}